//
//  AppDelegate+NI.m
//  Netease
//
//  Created by 王方伟 on 2017/8/18.
//  Copyright © 2017年 wfw. All rights reserved.
//

#import "AppDelegate+NI.h"
#import "SHAnchorPreviewController.h"
#import "SHLoginViewController.h"
#import "SHCellLayoutConfig.h"
#import "NTESClientUtil.h"
#import "SHDragView.h"
#import <NIMSDK/NIMSDK.h>


@interface AppDelegate ()<NIMLoginManagerDelegate>

@end

@implementation AppDelegate (NI)
- (BOOL)application:(UIApplication *)application niDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    [self setupNIMSDK];
    [self registerPushService];
    [self commonInitListenEvents];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPaneNotifion:) name:@"VideoPaneNotifion" object:nil];
    return YES;
}

#pragma mark 注册网易云
- (void)setupNIMSDK{
    [[NIMSDK sharedSDK] registerWithAppID:@"bfabcce01a73b68e6fdf065559e698b0" cerName:@""];
    //注册自定义消息的解析器
//    [NIMCustomObject registerCustomDecoder:[NTESCustomAttachmentDecoder new]];
    
    //注册 NIMKit 自定义排版配置
    //注册 NIMKit 自定义排版配置
    [[NIMKit sharedKit] registerLayoutConfig:[SHCellLayoutConfig new]];
}

#pragma mark 注册通知
- (void)registerPushService{
   [[[NIMSDK sharedSDK] loginManager] addDelegate:self];
}

#pragma mark 注册IM系统通知
- (void)commonInitListenEvents{
    
}

- (void)videoPaneNotifion:(NSNotification *)notifion{
    SHDragView *dragView = notifion.object;
    dragView.dragEnable = YES;
    dragView.frame = CGRectMake(SHScreenW - 160, 84, 150, 200);
    dragView.isBig = FALSE;
    [self.window addSubview:dragView];
    dragView.clickDragViewBlock = ^(SHDragView *dragView){
        if (dragView.isBig) return;
        SHAnchorPreviewController *anchorPreVC = [[SHAnchorPreviewController alloc] init];
        [DCURLRouter presentViewController:anchorPreVC animated:YES completion:^{
            dragView.isBig = YES;
        }];
    };
}

#pragma mark NIMLoginManagerDelegate
-(void)onKick:(NIMKickReason)code clientType:(NIMLoginClientType)clientType{
    NSString *reason = @"你被踢下线";
    switch (code) {
        case NIMKickReasonByClient:
        case NIMKickReasonByClientManually:{
            NSString *clientName = [NTESClientUtil clientName:clientType];
            reason = clientName.length ? [NSString stringWithFormat:@"你的帐号被%@端踢出下线，请注意帐号信息安全",clientName] : @"你的帐号被踢出下线，请注意帐号信息安全";
            break;
        }
        case NIMKickReasonByServer:
            reason = @"你被服务器踢下线";
            break;
        default:
            break;
    }
    [SVProgressHUD showInfo:reason];
    [[[NIMSDK sharedSDK] loginManager] logout:^(NSError *error) {
        [self.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
        self.window.rootViewController = [[SHLoginViewController alloc] init];
    }];
}
@end
